using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Runtime.Serialization;
using DIS_DAO.DAO;
using DIS_DAO.Helpers;

namespace DIS_DAO.DTO
{
	/// <summary>
	/// Data transfer object which is associated with OrderDetail table in database
	/// </summary>
	public class OrderDetailDTO
	{
		public int ID { get; set; }
		public int IDOrder { get; set; }
		public int IDItem { get; set; }
		public int Quantity { get; set; }
		public double Price { get; set; }
		public double Discount { get; set; }
        public ItemDTO Item { get; set; }
        public OrdersDTO Order { get; set; }

		public static OrderDetailDTO Translate(DataRow row)
		{
			var dto = new OrderDetailDTO
			{
				ID = int.Parse(row["ID"].ToString()),
				IDOrder = int.Parse(row["IDOrder"].ToString()),
				IDItem = int.Parse(row["IDItem"].ToString()),
				Quantity = int.Parse(row["Quantity"].ToString()),
				Price = double.Parse(row["Price"].ToString()),
				Discount = double.Parse(row["Discount"].ToString()),
			};

            var rpt = new ItemDAO(GlobalVariables.DbConnectionString);
            var objDS = rpt.SelectByID(dto.IDItem);
            if (objDS != null)
            {
                if (objDS.Tables[0].Rows.Count > 0)
                {
                    dto.Item = ItemDTO.Translate(objDS.Tables[0].Rows[0]);
                }
            }

            var rpt2 = new OrdersDAO(GlobalVariables.DbConnectionString);
            var objDS2 = rpt.SelectByID(dto.IDOrder);
            if (objDS2 != null)
            {
                if (objDS2.Tables[0].Rows.Count > 0)
                {
                    dto.Order = OrdersDTO.Translate(objDS2.Tables[0].Rows[0]);
                }
            }

			return dto;
		}
	}
}